package com.ys.ffms.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.ffms.common.Result;
import com.ys.ffms.mapper.BillMapper;
import com.ys.ffms.mapper.UserMapper;
import com.ys.ffms.pojo.Bill;
import com.ys.ffms.pojo.DO.BillDO;
import com.ys.ffms.pojo.PO.ExpensePO;
import com.ys.ffms.pojo.PO.IncomePO;
import com.ys.ffms.pojo.VO.queryBillVO;
import com.ys.ffms.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description: UserServiceImpl 实现类
 * @PROJECT_NAME: ffmsService
 * @DESCRIPTION:
 * @USER: lin
 * @DATE: 2021/12/24 13:51
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询 收入
     * @param pageNum
     * @param pageSize
     * @param details
     * @param titleKey
     * @return
     */
    @Override
    public Page<queryBillVO> findAllBill(Integer pageNum, Integer pageSize, String details, String titleKey) {
        Page<queryBillVO> page = new Page<>(pageNum,pageSize);
        Page<queryBillVO> page2 = billMapper.findAllByPage(page,details,titleKey);

        return page2;
    }

    /**
     * 分页查 支出
     * @param pageNum
     * @param pageSize
     * @param details
     * @param titleKey
     * @return
     */
    @Override
    public Page<queryBillVO> findAllBillExpenditure(Integer pageNum, Integer pageSize, String details, String titleKey) {
        Page<queryBillVO> page = new Page<>(pageNum,pageSize);
        Page<queryBillVO> page2 = billMapper.findAllByPageExpenditure(page, details, titleKey);
        return page2;
    }

    @Override
    public void addWater(BillDO billDO) {
        //初始化 Bill
        Bill bill = new Bill();
        //获取前台传来的UserName
        String userName = billDO.getUserName();
        System.err.println("---"+userName);
        //查出对应userID
        int userId = userMapper.findById(userName);
        //将数据补齐
        bill.setUserId(userId);
        bill.setPrice(billDO.getPrice());
        bill.setChannelId(billDO.getChannelId());
        bill.setStatus(1);//1.数据正常 2.数据移除
        bill.setTitle(billDO.getTitle());
        bill.setDetails(billDO.getDetails());
        bill.setFlowtypeId(2);//1.支出 2.收入
        bill.setCreateTime(new Date());
        //插入数据
         billMapper.insert(bill);
    }

    @Override
    public void delWater(Integer id) {
        billMapper.delWater(id);
    }

    @Override
    public int delWaterById(Integer id) {
      int jg = billMapper.delWaterById(id);
        return jg;
    }

    @Override
    public queryBillVO CheckOneById(Integer id) {
       queryBillVO bill = billMapper.CheckOneById(id);
        return bill;
    }

    @Override
    public List<queryBillVO> findAll() {
        return billMapper.findAll();
    }

    @Transactional
    @Override
    public Result editWater(queryBillVO queryBillVO) {
        //初始化账单
        Bill bill = new Bill();
        //获取payway的枚举值
        Integer payway = billMapper.checkOneByPay(queryBillVO.getPayway());
        System.err.println(queryBillVO.getPayway());
        System.err.println(payway);

        //补全信息
        bill.setId(queryBillVO.getId());
        bill.setUserId(queryBillVO.getUserId());
        bill.setPrice(queryBillVO.getPrice());
        bill.setChannelId(payway);
        bill.setStatus(1);
        bill.setTitle(queryBillVO.getTitle());
        bill.setDetails(queryBillVO.getDetails());
        bill.setFlowtypeId(2);
        bill.setCreateTime(queryBillVO.getCreateTime());

        //修改
        int i = billMapper.updateById(bill);
        if(i>0){
            return Result.success(bill,"修改成功");
        }else {
            return Result.error("500","未知错误!");
        }
    }

    @Override
    public void addWaterExpenditure(BillDO billDO) {
        //初始化 Bill
        Bill bill = new Bill();
        //获取前台传来的UserName
        String userName = billDO.getUserName();
        System.err.println("---"+userName);
        //查出对应userID
        int userId = userMapper.findById(userName);
        //将数据补齐
        bill.setUserId(userId);
        bill.setPrice(billDO.getPrice());
        bill.setChannelId(billDO.getChannelId());
        bill.setStatus(1);//1.数据正常 2.数据移除
        bill.setTitle(billDO.getTitle());
        bill.setDetails(billDO.getDetails());
        bill.setFlowtypeId(1);//1.支出 2.收入
        bill.setCreateTime(new Date());
        //插入数据
        billMapper.insert(bill);
    }

    @Override
    public Result editWaterExpenditure(queryBillVO queryBillVO) {
        //初始化账单
        Bill bill = new Bill();
        //获取payway的枚举值
        Integer payway = billMapper.checkOneByPay(queryBillVO.getPayway());
        System.err.println(queryBillVO.getPayway());
        System.err.println(payway);

        //补全信息
        bill.setId(queryBillVO.getId());
        bill.setUserId(queryBillVO.getUserId());
        bill.setPrice(queryBillVO.getPrice());
        bill.setChannelId(payway);
        bill.setStatus(1);
        bill.setTitle(queryBillVO.getTitle());
        bill.setDetails(queryBillVO.getDetails());
        bill.setFlowtypeId(1);
        bill.setCreateTime(queryBillVO.getCreateTime());

        //修改
        int i = billMapper.updateById(bill);
        if(i>0){
            return Result.success(bill,"修改成功");
        }else {
            return Result.error("500","未知错误!");
        }
    }

    @Override
    public List<IncomePO> findIncomeAll() {
        return billMapper.findIncomeAll();
    }

    @Override
    public List<ExpensePO> findExpenseExport() {
        return billMapper.findExpenseExport();
    }

    @Override
    public int findAllOrders() {
       int count = billMapper.findAllOrders();
        return count;
    }

    @Override
    public int findCleanWater() {
        //查出当天支出总额
        int expenditureNum = billMapper.findTotalExpenditure();//200
        //查出当天收入总额
        int incomeNum = billMapper.findTotalRevenue();//100
        return incomeNum - expenditureNum;
    }

}
